home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / RAVE DDK 1.0.6 GM for MacOS / Projects / Test Code / TinselTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  10.8 KB  |  506 lines  |  [TEXT/MPCC]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        TinselTest.c                                             **
  4.  **                                                                          **
  5.  **     Purpose:     Main file for Tinsel Town test app                         **
  6.  **                                                                          **
  7.  **     Author:        Mike W. Kelley                                             **
  8.  **                                                                             **
  9.  **                    2/3/95    Revised for 0.9 SDK release                         **
  10.  **                                                                          **
  11.  **     Copyright (C) 1994-95 Apple Computer, Inc.  All rights reserved.     **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. /* Macintosh */
  20. #include <Types.h>
  21. #include <QuickDraw.h>
  22. #include <Windows.h>
  23.  
  24. /* Project */
  25. #include "Drive3D.h"
  26. #include "TinselTest.h"
  27.  
  28. /*******************************************************************************************
  29.  *
  30.  * Defines that affect how the tests are run.
  31.  *
  32.  ******************************************************************************************/
  33.  
  34.  #define dWaitForButton        /* If defined, wait for button click between tests */
  35.  
  36. /*******************************************************************************************
  37.  *
  38.  * Declare the registration function for the empty rasterizer. This allows us to call
  39.  * it from our app, so we can link the rasterizer in instead of using a shared library.
  40.  *
  41.  ******************************************************************************************/
  42.  
  43. extern OSErr TtRegister (void);
  44.  
  45. /*******************************************************************************************
  46.  *
  47.  * Globals.
  48.  *
  49.  ******************************************************************************************/
  50.  
  51. long            gWindowWidth, gWindowHeight;
  52. TQAEngine        *gEngine [kTinselWindows];
  53. TQADrawContext    *gDrawContext [kTinselWindows];
  54. TQATexture        *gTexture [kTinselWindows];
  55.  
  56. /*******************************************************************************************
  57.  *
  58.  * Static variables used by this file's functions only.
  59.  *
  60.  ******************************************************************************************/
  61.  
  62. static float frand (void)
  63. {
  64.     return ((Random() + 32768) / 65536.0);
  65. }
  66.  
  67. /*******************************************************************************************
  68.  *
  69.  * Main. This runs the tests, clearing the windows before each test and waiting for
  70.  * a mouse click after each test. Each test is run four ways: Single and double buffered,
  71.  * with and without 2D clipping.
  72.  *
  73.  ******************************************************************************************/
  74.  
  75. int main()
  76. {
  77.     long        tinselTest, tinselStyle;
  78.     long        i;
  79.     TQAVGouraud    vGouraud[3];
  80.     TQAVTexture    vTexture[3];
  81.     long        flushCount;
  82.             
  83.     /*
  84.      * If we are running with a shared library, TdLinkEngine should not be defined!
  85.      */
  86.     
  87.     #ifdef dLinkEngine
  88.         (void) TtRegister();
  89.     #endif
  90.     
  91.     /*
  92.      * Create the test window(s).
  93.      */
  94.     
  95.     InitToolbox ();
  96.  
  97.     if ( ! CreateTestWindows (kTinselMaxWindowSize, kTinselMaxWindowSize))
  98.     {
  99.         return (-1);
  100.     }
  101.     if ( ! CreateWindowClip ())
  102.     {
  103.         return (-1);
  104.     }
  105.     if ( ! TestTextureNew())
  106.     {
  107.         return (-1);
  108.     }
  109.     
  110.     /*
  111.      * Run tests.
  112.      */
  113.     
  114.     for (tinselStyle = 0; tinselStyle <= kMaxStyle; ++tinselStyle)
  115.     {
  116.         switch (tinselStyle)
  117.         {
  118.             case kStyleSingleBuffered:
  119.                 if ( ! TestDrawContextNew (kQAContext_DeepZ, false))
  120.                 {
  121.                     return (-1);
  122.                 }
  123.                 break;
  124.             
  125.             case kStyleDoubleBuffered:
  126.                 if ( ! TestDrawContextNew (kQAContext_DeepZ | kQAContext_DoubleBuffer, false))
  127.                 {
  128.                     return (-1);
  129.                 }
  130.                 break;
  131.                 
  132.             case kStyleSingleBufferedClip:
  133.                 if ( ! TestDrawContextNew (kQAContext_DeepZ, true))
  134.                 {
  135.                     return (-1);
  136.                 }
  137.                 break;
  138.             
  139.             case kStyleDoubleBufferedClip:
  140.                 if ( ! TestDrawContextNew (kQAContext_DeepZ | kQAContext_DoubleBuffer, true))
  141.                 {
  142.                     return (-1);
  143.                 }
  144.                 break;
  145.         }
  146.  
  147.         ClearWindows();
  148.             
  149.         for (tinselTest = 0; tinselTest <= kMaxTest; ++tinselTest)
  150.         {
  151.             switch (tinselTest)
  152.             {
  153.                 case kTestPoints:
  154.                     TestRenderStart();
  155.                     for (i = 0, flushCount = kNPoints / kNFlushes; i++ < kNPoints; /* Nothing */)
  156.                     {
  157.                         TestRandomVGouraud (&vGouraud[0]);
  158.                         TestDrawPoint (&vGouraud[0]);
  159.                         if ((i % flushCount) == 0)
  160.                         {
  161.                             TestFlush();
  162.                         }
  163.                     }
  164.                     TestRenderEnd();
  165.                     break;
  166.                     
  167.                 case kTestLines:
  168.                     TestRenderStart();
  169.                     for (i = 0, flushCount = kNLines / kNFlushes; i++ < kNLines; /* Nothing */)
  170.                     {
  171.                         TestRandomVGouraud (&vGouraud[0]);
  172.                         TestRandomVGouraud (&vGouraud[1]);
  173.                         TestDrawLine (&vGouraud[0], &vGouraud[1]);
  174.                         if ((i % flushCount) == 0)
  175.                         {
  176.                             TestFlush();
  177.                         }
  178.                     }
  179.                     TestRenderEnd();
  180.                     break;
  181.                     
  182.                 case kTestTriGouraud:
  183.                     TestRenderStart();
  184.                     for (i = 0, flushCount = kNTris / kNFlushes; i++ < kNTris; /* Nothing */)
  185.                     {
  186.                         TestRandomVGouraud (&vGouraud[0]);
  187.                         TestRandomVGouraud (&vGouraud[1]);
  188.                         TestRandomVGouraud (&vGouraud[2]);
  189.                         
  190.                         TestDrawTriGouraud (&vGouraud[0],
  191.                                 &vGouraud[1], &vGouraud[2], kQATriFlags_None);
  192.                         
  193.                         if ((i % flushCount) == 0)
  194.                         {
  195.                             TestFlush();
  196.                         }
  197.                     }
  198.                     TestRenderEnd();
  199.                     break;
  200.                     
  201.                 case kTestTriTexture:
  202.                     TestRenderStart();
  203.                     TestSetStateTexture();
  204.                     for (i = 0, flushCount = kNTris / kNFlushes; i++ < kNTris; /* Nothing */)
  205.                     {
  206.                         /*
  207.                          * Setup the u,v values before calling TestRandomVTexture(),
  208.                          * as it references them.
  209.                          */
  210.                         
  211.                         vTexture[0].uOverW = 0.0;
  212.                         vTexture[0].vOverW = 0.0;
  213.  
  214.                         vTexture[1].uOverW = 0.0;
  215.                         vTexture[1].vOverW = 1.0;
  216.  
  217.                         vTexture[2].uOverW = 1.0;
  218.                         vTexture[2].vOverW = 1.0;
  219.  
  220.                         TestRandomVTexture (&vTexture[0]);
  221.                         TestRandomVTexture (&vTexture[1]);
  222.                         TestRandomVTexture (&vTexture[2]);
  223.                         
  224.                         TestDrawTriTexture (&vTexture[0],
  225.                                 &vTexture[1], &vTexture[2], kQATriFlags_None);
  226.                         
  227.                         if ((i % flushCount) == 0)
  228.                         {
  229.                             TestFlush();
  230.                         }
  231.                     }
  232.                     TestRenderEnd();
  233.                     break;
  234.             }
  235.             
  236.             #ifdef dWaitForButton
  237.                 /*
  238.                  * Wait for a mouse button click..
  239.                  */
  240.                 
  241.                 while ( ! Button())
  242.                     /* Do nothing */ ;
  243.                 FlushEvents (everyEvent, 0);
  244.             #endif
  245.         }
  246.         
  247.         TestDrawContextDelete();
  248.     }
  249.     
  250.     return (0);
  251. }
  252.  
  253. /*******************************************************************************************
  254.  *
  255.  * Set xyzw fields of a TQAVGouraud randomly. All other fields get simple defaults.
  256.  *
  257.  ******************************************************************************************/
  258.  
  259. void TestRandomVGouraud (
  260.     TQAVGouraud        *v)
  261. {
  262.     v->x = frand() * gWindowWidth;
  263.     v->y = frand() * gWindowHeight;
  264.     v->z = frand();
  265.     v->invW = 1.0 / v->z;
  266.     
  267.     v->a = 1.0;
  268.     v->r = frand();
  269.     v->g = frand();
  270.     v->b = frand();
  271. }
  272.  
  273. /*******************************************************************************************
  274.  *
  275.  * Set xyzw fields of a TQAVTexture randomly. The incoming u,v values are multiplied
  276.  * by the random invW value; all other fields get simple defaults.
  277.  *
  278.  ******************************************************************************************/
  279.  
  280. void TestRandomVTexture (
  281.     TQAVTexture        *v)
  282. {
  283.     v->x = frand() * gWindowWidth;
  284.     v->y = frand() * gWindowHeight;
  285.     v->z = frand();
  286.     v->invW = 1.0 / v->z;
  287.     
  288.     v->uOverW *= v->invW;
  289.     v->vOverW *= v->invW;
  290.     
  291.     v->a = 1.0;
  292.     v->r = 0.0;
  293.     v->g = 0.0;
  294.     v->b = 0.0;
  295.     
  296.     v->kd_r = 1.0;
  297.     v->kd_g = 1.0;
  298.     v->kd_b = 1.0;
  299.     
  300.     v->ks_r = 0.0;
  301.     v->ks_g = 0.0;
  302.     v->ks_b = 0.0;
  303. }
  304.  
  305. /*******************************************************************************************
  306.  *
  307.  * Miscellaneous test functions.
  308.  *
  309.  ******************************************************************************************/
  310.  
  311. void TestRenderStart (void)
  312. {
  313.     long        i;
  314.     
  315.     for (i = 0; i < kTinselWindows; ++i)
  316.     {
  317.         if (gDrawContext [i])
  318.         {
  319.             QARenderStart (gDrawContext [i], NULL, NULL);
  320.         }
  321.     }
  322. }
  323.  
  324. void TestRenderEnd (void)
  325. {
  326.     long        i;
  327.     TQAError    status;
  328.     
  329.     for (i = 0; i < kTinselWindows; ++i)
  330.     {
  331.         if (gDrawContext [i])
  332.         {
  333.             status = QARenderEnd (gDrawContext [i], NULL);
  334.         }
  335.     }
  336. }
  337.  
  338. void TestSetStateTexture (void)
  339. {
  340.     long        i;
  341.     
  342.     for (i = 0; i < kTinselWindows; ++i)
  343.     {
  344.         if (gDrawContext [i])
  345.         {
  346.             QASetPtr (gDrawContext [i], kQATag_Texture, gTexture [i]);
  347.             
  348.             /*
  349.              * NOTE: The current Apple software rasterizer always assumes these
  350.              * kQATextureOp_Modulate | kQATextureOp_Highlight are set, as that's
  351.              * the standard QuickDraw™ 3D mode. So if you change this, you may
  352.              * find that the software rasterizer isn't a good test case.
  353.              * (This will be fixed soon...)
  354.              */
  355.             
  356.             QASetInt (gDrawContext [i], kQATag_TextureOp,
  357.                     kQATextureOp_Modulate | kQATextureOp_Highlight);
  358.         }
  359.     }
  360. }
  361.  
  362. void TestFlush (void)
  363. {
  364.     long        i;
  365.     
  366.     for (i = 0; i < kTinselWindows; ++i)
  367.     {
  368.         if (gDrawContext [i])
  369.         {
  370.             QAFlush (gDrawContext [i]);
  371.         }
  372.     }
  373. }
  374.  
  375. void TestDrawPoint (
  376.     const TQAVGouraud    *v)
  377. {
  378.     long        i;
  379.     
  380.     for (i = 0; i < kTinselWindows; ++i)
  381.     {
  382.         if (gDrawContext [i])
  383.         {
  384.             QADrawPoint (gDrawContext [i], v);
  385.         }
  386.     }
  387. }
  388.  
  389. void TestDrawLine (
  390.     const TQAVGouraud    *v0,
  391.     const TQAVGouraud    *v1)
  392. {
  393.     long        i;
  394.     
  395.     for (i = 0; i < kTinselWindows; ++i)
  396.     {
  397.         if (gDrawContext [i])
  398.         {
  399.             QADrawLine (gDrawContext [i], v0, v1);
  400.         }
  401.     }
  402. }
  403.  
  404. void TestDrawTriGouraud (
  405.     const TQAVGouraud    *v0,
  406.     const TQAVGouraud    *v1,
  407.     const TQAVGouraud    *v2,
  408.     unsigned long        flags)
  409. {
  410.     long        i;
  411.     
  412.     for (i = 0; i < kTinselWindows; ++i)
  413.     {
  414.         if (gDrawContext [i])
  415.         {
  416.             QADrawTriGouraud (gDrawContext [i], v0, v1, v2, flags);
  417.         }
  418.     }
  419. }
  420.  
  421. void TestDrawTriTexture (
  422.     const TQAVTexture    *v0,
  423.     const TQAVTexture    *v1,
  424.     const TQAVTexture    *v2,
  425.     unsigned long        flags)
  426. {
  427.     long        i;
  428.     
  429.     for (i = 0; i < kTinselWindows; ++i)
  430.     {
  431.         /*
  432.          * Test both for a draw context, and that a texture was created.
  433.          */
  434.         
  435.         if (gDrawContext [i] && gTexture [i])
  436.         {
  437.             QADrawTriTexture (gDrawContext [i], v0, v1, v2, flags);
  438.         }
  439.     }
  440. }
  441.  
  442. Boolean TestTextureNew (void)
  443. {
  444.     TexturePixel        *pixmap;
  445.     long                x, y;
  446.     long                i;
  447.     
  448.     /*
  449.      * Create the source pixmap. Note that we can't free it (unless we
  450.      * called QATextureDetach()). We just do a procedural map with some
  451.      * checkerboard effect...
  452.      */
  453.     
  454.     if ( ! (pixmap = malloc (sizeof (*pixmap) * kTextureSize * kTextureSize)))
  455.     {
  456.         return (false);
  457.     }
  458.     
  459.     for (x = 0; x < kTextureSize; ++x)
  460.     {
  461.         for (y = 0; y < kTextureSize; ++y)
  462.         {
  463.             pixmap [x + (y << kTextureSizeNBits)].a = 0xff;
  464.             pixmap [x + (y << kTextureSizeNBits)].r = (x & 0x1f) << 3;
  465.             pixmap [x + (y << kTextureSizeNBits)].g = (y & 0x1f) << 3;
  466.             pixmap [x + (y << kTextureSizeNBits)].b = (((x + y) / 2) & 0x1f) << 3;
  467.         }
  468.     }
  469.  
  470.     for (i = 0; i < kTinselWindows; ++i)
  471.     {
  472.         if (gEngine [i])
  473.         {
  474.             /*
  475.              * Create the texture. If this fails, gTexture[i] will be NULL.
  476.              * This is ok, as TestDrawTriTexture() always tests it before calling
  477.              * QADrawTriTexture().
  478.              */
  479.             
  480.             gTexture [i] = CreateMipMapTexture (
  481.                     gEngine [i],                                /* store context */
  482.                     pixmap,                                        /* high res pix map */
  483.                     kTextureSize * sizeof (*pixmap),            /* row bytes */
  484.                     kTextureSizeNBits,                            /* widthNBits */
  485.                     kTextureSizeNBits);                            /* heightNBits */
  486.         }
  487.     }
  488.     
  489.     return (true);
  490. }
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.